package de.so.myteach;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class PupilListActivity extends FragmentActivity
  implements PupilListFragment.Callbacks, OnNavigationListener
{
  private boolean mTwoPane;
  private ArrayAdapter arrayAdapter;
  PupilDetailFragment PupilDetail;

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_pupil_list);
    
    ActionBar localActionBar = getActionBar();
    localActionBar.setDisplayShowTitleEnabled(false);
    localActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    arrayAdapter = new ArrayAdapter<String>(getActionBarThemedContextCompat(),
			android.R.layout.simple_list_item_1,
			android.R.id.text1, new String[] {
					getString(R.string.title_section1),
					getString(R.string.title_section2),
					getString(R.string.title_section3), });
    
    localActionBar.setListNavigationCallbacks(arrayAdapter, this);
    
  	if (findViewById(R.id.pupil_detail_container) != null)
    {
      this.mTwoPane = true;
      ((PupilListFragment)getSupportFragmentManager().findFragmentById(R.id.pupil_list)).setActivateOnItemClick(true);
    }
  }
  
  /**
	 * Backward-compatible version of {@link ActionBar#getThemedContext()} that
	 * simply returns the {@link android.app.Activity} if
	 * <code>getThemedContext</code> is unavailable.
	 */
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private Context getActionBarThemedContextCompat() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			return getActionBar().getThemedContext();
		} else {
			return this;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

  public void onItemSelected(String paramString)
  {
    if (this.mTwoPane)
    {
      //if (PupilDetail != null) {getSupportFragmentManager().beginTransaction().remove(PupilDetail).commit(); }
      Bundle bundle = new Bundle();
      bundle.putString("item_id", paramString);
      PupilDetail = new PupilDetailFragment();
      PupilDetail.setArguments(bundle);
      getSupportFragmentManager().beginTransaction().replace(R.id.pupil_detail_container, PupilDetail, PupilDetail.getClass().getName().intern()).commit();
    }
    else {
      Intent intent = new Intent(this, PupilDetailActivity.class);
      intent.putExtra("item_id", paramString);
      startActivity(intent);
    }
  }

  public boolean onNavigationItemSelected(int Pos, long Id)
  {
	
//	((PupilListFragment)getSupportFragmentManager()
    if (this.mTwoPane)
    	onItemSelected("0");
//    String str = (String) arrayAdapter.getItem(Pos);
    Bundle bundle = new Bundle();
    bundle.putInt("list_id", Pos);
    PupilListFragment PupilList = new PupilListFragment();
    PupilList.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.pupil_list, PupilList).commit();
//    ((PupilListFragment)getSupportFragmentManager().findFragmentById(R.layout.activity_pupil_list)).setChanged();
//    ((PupilListFragment)getSupportFragmentManager().findFragmentById(R.layout.activity_pupil_list)).setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS));
//    Toast.makeText(this, "Clicked NavigationItem "+Pos + "  "+ str.toString(), Toast.LENGTH_SHORT).show();
    return true;
  }

}
